class Solution {
    int dx[4] = { 0, 0, 1, -1 }, dy[4] = { 1, -1, 0, 0 }, m, n, ret;
    bool visited[50][50];
    void dfs(vector<vector<int>>& grid, int i, int j, int &tmp)
    {
        tmp++;
        visited[i][j] = true;
        for(int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if(x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 && visited[x][y] == false)
                dfs(grid, x, y, tmp);
        }
    }
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        m = grid.size(), n = grid[0].size();
        int ret = 0;
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(grid[i][j] == 1 && visited[i][j] == false)
                {
                    int tmp = 0;
                    dfs(grid, i, j, tmp);
                    ret = max(ret, tmp);
                }
            }
        }
        return ret;
    }
};